/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.module.properties;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import org.w3c.dom.NodeList;
import tal.drivechain.chain.ChainNode;

/**
 *
 * @author Main
 */
public class DateParser implements PropertyParser {

    public String getPropertyType() {
        return "date";
    }

    public void setAttribures(HashMap<String, String> attributes, NodeList childeElements) throws IllegalArgumentException {
    }

    public Object parseProperty(String prop) throws IllegalArgumentException {
        Calendar then = Calendar.getInstance();
        Calendar now = Calendar.getInstance();
        try {
            SimpleDateFormat runat = new SimpleDateFormat("d.M.y,H:m:s");
            then.setTime(runat.parse(prop));
        } catch (java.text.ParseException pe0) {
            try {
                SimpleDateFormat runat = new SimpleDateFormat("H:m:s");
                then.setTime(runat.parse(prop));
                then.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE));
            } catch (java.text.ParseException pe) {
                throw new IllegalArgumentException("Invalid date/time format: " + pe.getMessage());
            }
        }
        return then;
    }

    public String propToString(Object property, ChainNode context) {
        Calendar date = (Calendar)property;
        SimpleDateFormat format = new SimpleDateFormat("d.M.y,H:m:s");
        return format.format(date);
    }

}
